package com.example.recite_words.util;

import static android.os.Environment.MEDIA_MOUNTED;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class RWfile {

     public static OutputStream readfile(String path) throws FileNotFoundException {
          File file = new File(path);
          return new FileOutputStream(file);
     }
     public static void copyfile(InputStream sourceFile, String name) throws IOException {
         File file =  createFileIfNeed(name);
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
               FileUtils.copy(sourceFile,new FileOutputStream(file));
          }
     }


     public static File createFileIfNeed(String fileName) throws IOException {
          String fileA = getPath();
          File fileJA = new File(fileA);
          if (!fileJA.exists()) {
               fileJA.mkdirs();
          }
          File file = new File(fileName);
          if (!file.exists()) {
               file.createNewFile();
          }
          return file;
     }
     public static String getPath(){
          File extDir = Environment.getExternalStorageDirectory();
          String fileA = extDir.getAbsolutePath();
          return fileA;
     }


     public static boolean deleteFile(String filePath) {
          File file = new File(filePath);
          if (file.isFile() && file.exists()) {
               return file.delete();
          }
          return false;
     }





}
